{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# FFI 设备"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pytest\n",
    "import pickle\n",
    "from tvm.ffi import Device\n",
    "from tvm import ffi as tvm_ffi"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## {class}`tvm.ffi.Device`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "device = tvm_ffi.Device(\"cuda\", 0)\n",
    "assert device.device_type == tvm_ffi.Device.kDLCUDA\n",
    "assert device.device_id == 0\n",
    "assert str(device) == \"cuda:0\"\n",
    "assert device.__repr__() == \"device(type='cuda', index=0)\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "device = tvm_ffi.device(\"ext_dev:0\")\n",
    "assert device.device_type == tvm_ffi.Device.kDLExtDev\n",
    "assert device.device_id == 0\n",
    "assert str(device) == \"ext_dev:0\"\n",
    "assert device.__repr__() == \"device(type='ext_dev', index=0)\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "device(type='cpu', index=0)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dev = tvm_ffi.device(dev_type=\"cpu\", dev_id=0)\n",
    "dev"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "device = tvm_ffi.device(\"cuda\", 0)\n",
    "device_pickled = pickle.loads(pickle.dumps(device))\n",
    "assert device_pickled.device_type == device.device_type\n",
    "assert device_pickled.device_id == device.device_id"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ai",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
